package com.share51.novel.job;

import com.share51.novel.common.StatusConstant;
import com.share51.novel.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 同步小说章节内容的job
 * <p>每天定时爬取收录的小说列表，然后判断是否有新发布的章节，同步过来<p/>
 *
 * @author gb
 */
@Slf4j
@DisallowConcurrentExecution
public class SyncNovelChapterJob extends QuartzJobBean {

    @Resource
    private TaskService taskService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        boolean isRunning = jobExecutionContext.getTrigger().getJobDataMap().getBoolean(StatusConstant.QUARTZ_RUNNING_STATUS);
        if (!isRunning){
            jobExecutionContext.getTrigger().getJobDataMap().put(StatusConstant.QUARTZ_RUNNING_STATUS,true);
            log.info("开始同步已收录的小说和已发布的小说章节");
            long startTime = System.currentTimeMillis();
            try {
                taskService.syncNovelChapter();
                long endTime = System.currentTimeMillis();
                log.info("同步已收录的小说和已发布的小说章节结束,耗时：{}秒",(endTime-startTime)/1000);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                jobExecutionContext.getTrigger().getJobDataMap().put(StatusConstant.QUARTZ_RUNNING_STATUS,false);
            }
        }

    }
}
